<template>
  <el-container class="el-container-h">
    <el-main v-loading="loading">
      <el-select v-model="selectRelation" placeholder="请选择" class="main-select" @change="changeRelation">
        <el-option
          v-for="(item, key) in relation"
          :key="key"
          :label="item.name"
          :value="item.uid"
        />
      </el-select>
      <div ref="abc" class="table_div">
        <el-table
          ref="multipleTable"
          border
          size="mini"
          height="100%"
          class="table_relation"
          :data="targetData"
          tooltip-effect="dark"
        >
          <el-table-column fixed width="150" label-class-name="table_top">
            <el-table-column fixed width="150" class-name="table_l">
              <template slot-scope="scope">
                <span>{{ scope.row.item_name }}</span>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column fixed label-class-name="table_top_r">
            <el-table-column fixed width="80" show-overflow-tooltip class-name="table_r">
              <template slot-scope="scope">
                <span>{{ scope.row.item_id }}</span>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column
            v-for="(item, key) in originData"
            :key="key"
            :label="item.item_name"
            label-class-name="table_cell_name"
          >
            <el-table-column
              :label="item.item_id"
              label-class-name="table_cell_id"
              class-name="table_icon"
              min-width="100"
            >
              <template slot-scope="scope">
                <span><i :class="[relationData.find(n => n.origin_uid===item.item_uid && n.target_uid===scope.row.item_uid)?'el-icon-circle-check':'']" /></span>
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>
      </div>
    </el-main>
  </el-container>
</template>
<script>
import { mapState } from 'vuex'
export default {
  name: 'SystemRelation',
  data() {
    return {
      originData: [],
      targetData: [],
      relationData: [],
      selectRelation: null,
      // 显示参数
      tableTag: {
        time: '时间'
      },
      loading: false
    }
  },
  computed: {
    ...mapState({
      service: state => state.service,
      relation: state => state.systemProfile.relation
    })
  },
  created() {
    this.initData()
  },
  beforeUpdate() {
    this.$nextTick(() => {
      this.$refs.abc.children[0].style = `height:100%;`
    })
  },
  updated() {
    this.$nextTick(() => {
      const kbHeight = this.$refs.abc.children[0].children[3].children[1].offsetHeight - this.$refs.abc.children[0].children[3].children[1].children[0].offsetHeight
      if (kbHeight > 0) {
        this.$refs.abc.children[0].style = `height:${this.$refs.abc.children[0].offsetHeight - kbHeight}px;`
      }
    })
  },
  methods: {
    // 初始化
    initData() {
      this.selectRelation = this.relation[0].uid || null
      if (this.selectRelation) {
        this.changeRelation(this.selectRelation)
      }
    },
    // 获取origin
    async getCiOrigin(data) {
      const result = await this.service.getCi({ category_uid: data.origin_uid })
      if (result.flag) {
        this.originData = result.data || []
      }
    },
    // 获取target
    async getCiTarget(data) {
      const result = await this.service.getCi({ category_uid: data.target_uid })
      if (result.flag) {
        this.targetData = result.data || []
      }
    },
    // 获取relation
    async getCiRelation(data) {
      const result = await this.service.getCiRelation({ relation_uid: data.uid })
      if (result.flag) {
        this.relationData = result.data || []
      }
    },
    async changeRelation(data) {
      this.loading = true
      const getData = this.relation.find(n => n.uid === data) || {}
      this.getCiOrigin(getData)
      this.getCiTarget(getData)
      await this.getCiRelation(getData)
      this.loading = false
    }
  }
}
</script>
<style>
.explorer-content {
  position: relative;
}
</style>
<style scoped>
.el-container-h {
  width: 100%;
  height: 100%;
  position: absolute;
}
.el-main {
  position: relative;
}
.main-select {
  position: absolute;
  z-index: 5;
  left: 2.3rem;
  top: 2.3rem;
}
.el-container-h /deep/ .table_icon {
  text-align: center;
}
.el-container-h /deep/ th.table_l.is-leaf {
  border-right: none;
}
.el-container-h /deep/ th.table_top {
  border-bottom: none;
  border-right: none;
}
.el-container-h /deep/ th.table_top_r {
  border-bottom: none;
}
.el-container-h /deep/ .table_l,
.el-container-h /deep/ .table_r {
  background: #f5f7fa;
}
.el-container-h /deep/ .table_cell_id {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #606266;
  font-weight: normal;
  text-align: left;
}
.el-container-h /deep/ .table_cell_name {
  color: #606266;
  font-weight: normal;
}
.el-container-h /deep/ .table {
  width: calc(100% - 20px);
  max-height: calc(100% - 20px);
  position: absolute;
  top: 20px;
  left: 20px;
}
.table_div {
  width: calc(100% - 40px);
  height: calc(100% - 40px);
  position: absolute;
  overflow: hidden;
  top: 20px;
  left: 20px;
}
.table_div /deep/ .table_relation {
  width: 100%;
  max-height: 100%;
  position: absolute;
}
</style>
